﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dycox.ComponentModel
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class DisplayFormatAttribute : Attribute
    {
        public DisplayFormatAttribute(string format)
        {
            _format = format;
        }

        private string _format;

        public string Format
        {
            get { return _format; }
            set { _format = value; }
        }

        private string _nullText;

        public string NullText
        {
            get { return _nullText; }
            set { _nullText = value; }
        }

        public string FormatValue(object value)
        {
            return FormatValue(value, System.Globalization.CultureInfo.CurrentCulture);
        }

        public virtual string FormatValue(object value, IFormatProvider provider)
        {
            if (value == null)
                return _nullText ?? string.Empty;

            if (_format != null)
                return string.Format(provider, _format, value);
            else
                return value.ToString();
                
        }
    }
}
